// pages/add/add.js
Page({
  data: {
    name: '',
    date: '',
    year: '',
    month: '',
    day: ''
  },

  onLoad() {
    const now = new Date();
    const year = now.getFullYear();
    const month = (now.getMonth() + 1).toString().padStart(2, '0');
    const day = now.getDate().toString().padStart(2, '0');
    this.setData({ year, month, day, date: `${year}-${month}-${day}` });
  },

  onNameInput(e) {
    this.setData({ name: e.detail.value.trim() });
  },

  onDateChange(e) {
    this.setData({ date: e.detail.value });
  },

  saveMemorial() {
    const { name, date } = this.data;
    if (!name) return wx.showToast({ title: '请输入名称', icon: 'none' });
    if (!date) return wx.showToast({ title: '请选择日期', icon: 'none' });

    const id = Date.now().toString();
    const list = wx.getStorageSync('memorialList') || [];
    list.unshift({ id, name, date });
    wx.setStorageSync('memorialList', list);
    
    // 触发全局更新事件
    const app = getApp();
    app.eventBus.emit('memorial-updated');
    
    wx.showToast({ title: '保存成功' });
    setTimeout(wx.navigateBack, 1500);
  }
});
